<template>
  <div >
    <div class="welcomeTitle">
      <span>
        欢迎注册为用户！
      </span>
    </div>

    <div class="userInput">
      <el-form :model="userInfo" label-width="100px" :inline="true" :rules="rules"
               v-loading="registerLoading" ref="userInfo">
        <el-form-item label="手机号码：" prop="phone">
          <el-input v-model="userInfo.phone" placeholder="输入手机号码"></el-input>
        </el-form-item>

        <el-form-item label="姓名：" prop="name">
          <el-input v-model="userInfo.name" placeholder="输入姓名"></el-input>
        </el-form-item>

        <el-form-item label="性别：" prop="sex">
          <el-select v-model="userInfo.sex" placeholder="请选择性别">
            <el-option v-for="item in sexOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="生日：" prop="birthday">
          <el-date-picker
              v-model="userInfo.birthday"
              value-format="yyyy-MM-dd"
              type="date" placeholder="选择生日"
              width="100px"></el-date-picker>
        </el-form-item>

        <el-form-item label="QQ：" prop="qq">
          <el-input v-model="userInfo.qq" placeholder="输入QQ"></el-input>
        </el-form-item>

        <el-form-item label="微信：" prop="wx">
          <el-input v-model="userInfo.wx" placeholder="输入微信"></el-input>
        </el-form-item>

        <el-form-item label="邮箱：" prop="email">
          <el-input v-model="userInfo.email" placeholder="输入邮箱"></el-input>
        </el-form-item>

        <el-form-item label="地址：" prop="addr">
          <el-input v-model="userInfo.addr" placeholder="输入地址"></el-input>
        </el-form-item>

        <el-form-item label="密码：" prop="pwd">
          <el-input v-model="userInfo.pwd" placeholder="输入密码" show-password></el-input>
        </el-form-item>

        <el-form-item label="确认密码：" prop="rePwd">
          <el-input v-model="userInfo.rePwd" placeholder="确认密码" show-password></el-input>
        </el-form-item>

        <el-form-item label="验证码：" prop="verifyCode">
          <div class="imgDiv">
            <el-input v-model="userInfo.verifyCode" placeholder="输入验证码"></el-input>
          </div>

          <img :src="verifiCodeUrl" alt="验证码" @click="changeUrl"/>
        </el-form-item>

      </el-form>
    </div>

    <div>
      <el-button type="success" @click="register" :disabled="registerLoading">注册</el-button>
      <el-button type="info" @click="back">返回登录</el-button>
    </div>
  </div>
</template>

<script>
import loginApi from "@/service/loginApi";

export default {

  name: "register",
  data() {
    let checkPassword = (rule, value, callback) =>{
      if(value !== this.userInfo.pwd){
        callback(new Error("两次输入的密码不一致"));
      }
    }

    return {
      verifiCodeUrl: "/chen/api/getVerificationCode?t="+Math.random(),
      registerLoading: false,
      sexOption: [
        {
          value: "man",
          label: "男"
        },
        {
          value: "woman",
          label: "女"
        }
      ],

      userInfo: {
        name: "",
        sex: "",
        birthday:"",
        addr: "",
        phone: "",
        qq: "",
        wx: "",
        pwd: "",
        rePwd:"",
        email: "",
        verifyCode: ""
      },

       rules: {
        phone: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
         name: [
           {required: true, message: '请输入姓名', trigger: 'blur'}
         ],
         pwd: [
           {required: true, message: '请输入密码', trigger: 'blur'}
         ],
         rePwd: [
           {required: true, message: '请确认密码', trigger: 'blur'},
           {validator: checkPassword, trigger: 'blur'}
         ],
         verifyCode: [
           {required: true, message: '请验证码', trigger: 'blur'}
         ],
         sex: [
           {required: true, message: '请输入性别', trigger: 'blur'}
         ],
         email: [
           {required: true, message: '请输入邮箱', trigger: 'blur'}
         ]
       }
    };
  },

  methods: {
    back() {
      this.$router.push("/");
    },

    changeUrl() {
      this.verifiCodeUrl = "/chen/api/getVerificationCode?t="+Math.random();
    },

    register(){
      //请求发送
      let that = this;
      this.registerLoading = true;
      let check = true;
      this.$refs.userInfo.validate((valid) => {

        if(!valid){
          that.registerLoading = false;
          check = false;
          return false;
        }else{
          check = true;
          return true;
        }
      });

      if(!check){
        console.error("验证不通过！！", check);
        return;
      }


      loginApi.register(this.userInfo).then(res => {
        if(res.data.code == 200){
          that.$message({message: res.data.message, type:"success"});
          that.changeUrl();
          setTimeout(()=>{
            that.$router.push("/");
          }, 1000);
        }else{
          that.$message({message: res.data.message, type:"error"});
          that.registerLoading = false;
          that.changeUrl();
        }
      }).catch(reason => {
        that.registerLoading = false;
        that.changeUrl();
      })
    }
  }
}
</script>

<style scoped>
    .welcomeTitle{
      font-size: 50px;
      text-align: center;
    }

    .userInput {
      margin-top: 20px;
      margin-left: 20%;
      width: 700px;
      border: green solid 1px;
      border-radius: 10px;
      padding: 20px;
      margin-bottom: 20px;
    }

    .imgDiv {
      width: 150px;
      display: inline-block;
    }
</style>